<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible="dialogVisible"
    title="查看扫描清单"
    width="90%"
    @close="$emit('close')"
  >
    <!--工具栏-->
    <div class="crud-group">
      <!-- 搜索 -->
      <el-select
        v-model="query.fileAffiliation"
        clearable
        size="small"
        placeholder="请选择归属"
        class="filter-item"
      >
        <el-option
          v-for="item in fileAffiliationList"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-input
        v-model="query.scanFileName"
        clearable
        size="small"
        placeholder="文件名称"
        style="width:200px"
        class="filter-item"
      />
      <rrOperation :crud="crud" />
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      border
      max-height="350px"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column
        label="文件归属"
        prop="fileAffiliation"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.fileAffiliation === 'init'">初始化脚本</span>
          <span v-if="scope.row.fileAffiliation === 'period'">周期脚本</span>
          <span v-if="scope.row.fileAffiliation === 'service'">下载、推送、稽核脚本</span>
        </template>
      </el-table-column>
      <el-table-column
        label="文件名称"
        prop="scanFileName"
      />
      <el-table-column
        label="扫描状态"
        prop="scanState"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.scanState==='SUCCESS'"
            size="small"
            type="success"
          >成功</el-tag>
          <el-tag
            v-if="scope.row.scanState==='SCANNING'"
            size="small"
          >扫描中</el-tag>
          <el-tag
            v-if="scope.row.scanState==='UNSCAN'"
            size="small"
            type="info"
          >待扫描</el-tag>
          <el-tag
            v-if="scope.row.scanState==='FAIL'"
            size="small"
            type="danger"
          >异常</el-tag>
          <el-tag
            v-if="scope.row.scanState==='WARNING'"
            size="small"
            type="warning"
          >告警</el-tag>
          <el-tag
            v-if="scope.row.scanState==='IGNORE'"
            size="success"
          >告警已忽略</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="扫描开始时间"
        prop="createTime"
      />
      <el-table-column
        label="扫描结束时间"
        prop="lastUpdateTime"
      />
      <el-table-column label="扫描明细">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="lookScanList(scope.row)"
          >查看扫描明细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <!--分页组件-->
      <pagination />
    </div>

    <scan-detail
      :scan-list-id="scanListId"
      :dialog-visible="scanDetailDialogVisible"
      :scan-file-name="scanFileName"
      @close="()=>{
        scanDetailDialogVisible=false
        cancelCU()
      }"
    />
  </el-dialog>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import scriptPackScanListApi from '@/api/script/pack/scriptPackScanList'
import scanDetail from '@/views/scriptPublish/scanDetail/index'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '脚本扫描确认清单', url: 'api/scriptPack/list/page', crudMethod: { ...scriptPackScanListApi } })
const defaultForm = {}
export default {
  name: 'ScriptCheckResult',
  components: { rrOperation, pagination, scanDetail },
  props: {
    packTaskId: {
      type: String,
      default: null
    },
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      scanFileName: '',
      scanListId: null,
      lookSacnDetailFlag: false,
      scanDetailDialogVisible: false,
      fileAffiliationList: [
        { key: 'init', value: '初始化脚本' },
        { key: 'period', value: '周期脚本' },
        { key: 'service', value: '下载、推送、稽核脚本' }
      ]
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.page = 0
        this.crud.resetQuery()
      }
    }
  },
  created(){
    this.crud.resetQuery(true)
  },
  methods: {
    [CRUD.HOOK.beforeRefresh](crud) {
      crud.query.scriptPackTaskId = this.packTaskId
    },
    lookScanList(row) {
      this.scanDetailDialogVisible = true
      this.scanListId = row.id
      this.scanFileName = row.scanFileName
    },
    cancelCU() {
      this.lookSacnDetailFlag = false
    }
  }
}
</script>
<style scoped>
</style>
